package tdd.xuchun.test.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

import cn.hutool.Hutool;
import cn.hutool.core.util.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import tdd.xuchun.test.model.Host;
import tdd.xuchun.test.model.Page;
import tdd.xuchun.test.model.Result;
import tdd.xuchun.test.service.IEasyNmonService;
import tdd.xuchun.test.service.IHostService;

/**
 * 主机控制器
 * 
 * @author ZSL
 *
 */
@RestController

public class HostController{

	/**
	 * 注入主机业务服务
	 */
	@Autowired
	private IHostService hostService;
	@Autowired
	private IEasyNmonService easyNmonService;

	// 映射"/"请求
	@RequestMapping("/")
	public ModelAndView index(){
		return new ModelAndView("index",new HashMap());
	}

	// 映射"/"请求
	@RequestMapping("/host/host_add")
	public ModelAndView hostadd(){
		return new ModelAndView("host_add",new HashMap());
	}
	/**
	 * 查询单个主机详细信息
	 * 
	 * @return
	 */
	@RequestMapping("host/editpage")
	public ModelAndView findHost(Host host){
		Host rsHost = hostService.getHost(host);
        Map<String,Object> rsMap = new HashMap<String,Object>();
        rsMap.put("data", rsHost);
		return new ModelAndView("host_edit",rsMap);
	}
	

	/**
	 * 查询单个主机详细信息
	 * 
	 * @return
	 */
	@RequestMapping("host/copyaddpage")
	public ModelAndView findAddHost(Host host){
		Host rsHost = hostService.getHost(host);
        Map<String,Object> rsMap = new HashMap<String,Object>();
        rsMap.put("data", rsHost);
		return new ModelAndView("host_copyadd",rsMap);
	}
	
	/**
	 * 增加新主机
	 * 
	 * @return
	 */
	@RequestMapping("host/add")
	public Result addHost(Host host){
		hostService.addHost(host);
		return new Result();
	}
	
	/**
	 * 删除主机
	 * 
	 * @return
	 */
	@RequestMapping("host/del")
	public Result delHost(Host host){
		host = hostService.getHost(host);
		int code = easyNmonService.accessTheURL(host, "");
		if (code == 200) {
			easyNmonService.clearService(host);
		}
		hostService.delHost(host);
		return new Result();
	}
	
	/**
	 * 编辑主机
	 * 
	 * @return
	 */
	@RequestMapping("host/edit")
	public Result editHost(Host host){
		hostService.editHost(host);
		return new Result();
	}
	
	/**
	 * 模糊查询主机列表
	 * 
	 * @return
	 */
	@RequestMapping("host/getcond")
	public Page queryLikeHosts(int pageSize,int pageNum,Host host){
		Map<String,Object> cond=new HashMap<String,Object>();
		cond.put("pageSize", pageSize);
		cond.put("pageNum", pageNum);
		cond.put("host", host);
		Page page = hostService.queryLikeHosts(cond);
		return page;
	}
	@RequestMapping("/upload")
	@ResponseBody
	public String upload(@RequestParam("file") MultipartFile file) {
		String fileName = file.getOriginalFilename();
		if(fileName.indexOf("\\") != -1){
			fileName = fileName.substring(fileName.lastIndexOf("\\"));
		}
		String filePath = "/tmp/"+IdUtil.simpleUUID()+"/";
		File targetFile = new File(filePath);
		if(!targetFile.exists()){
			targetFile.mkdirs();
		}
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(filePath+fileName);
			out.write(file.getBytes());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			return "上传失败";
		}
		return "上传成功!|"+filePath+fileName;
	}
}
